#include "MainMenu.h"

// Initialize the instance to null
MainMenu* MainMenu::m_MenuInstance = 0;

MainMenu::MainMenu(void)
{
}

MainMenu::~MainMenu(void)
{
}

MainMenu* MainMenu::GetInstance(void)
{
	// Create the singleton if it doesn't exist
	if(!m_MenuInstance)
		m_MenuInstance = new MainMenu;

	// Return the singleton
	return m_MenuInstance;
}

void MainMenu::DeleteMenuInstance(void)
{
	// If instance exist, delete it!
	if(m_MenuInstance)
	{
		delete m_MenuInstance;
		m_MenuInstance = NULL;
	}
}

void MainMenu::InitMainMenu(IDirect3DDevice9* m_pD3DDevice, HWND hWnd, HINSTANCE hInst)
{
	// Get the instance of the sprite manager
	m_pSpriteManager = SpriteManager::GetInstance();

	// Initialize the boolean to draw the main menu
	sMenuState = MAINMENU;
	sGameState = 0;
	sClickTimer = 0;

	// Initialize the timer
	lStartTime = (LONG)timeGetTime();

	// Initialize sound variable to default constructor
	m_pSound = Sound::GetSoundInstance();
	bSoundLoaded = false;

	// Initialize HighScore variable to default constructor
	m_pHighScore = HighScore::GetInstance();
	m_pHighScore->ReadFromFile();
	m_pHighScore->SortBoard();
	m_pHighScore->WriteToFile();

	// Initialize Text variable to default constructor
	m_pText = Text::GetInstance();

	// Main Menu Sprites
	sTitleBar = m_pSpriteManager->LoadSprite("MenuSprites\\titleBar.png", D3DXVECTOR3(300, 50, 0), 1.0f);
	sHPlayGame = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightPlay.png", D3DXVECTOR3(-50, -50, 0), 1.1f);
	sHCredits = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightCredits.png", D3DXVECTOR3(-50, -50, 0), 1.0f);
	sHInstructions = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightInstructions.png", D3DXVECTOR3(-50, -50, 0), 1.0f);
	sHHighScore = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightHighScore.png", D3DXVECTOR3(-50, -50, 0), 1.1f);
	sHQuit = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\menuHighlight.png", D3DXVECTOR3(-50, -50, 0), 1.0f);
	sPlayGame = m_pSpriteManager->LoadSprite("MenuSprites\\playButton.png", D3DXVECTOR3(130, 220, 0), 1.0f);
	sCredits = m_pSpriteManager->LoadSprite("MenuSprites\\creditsButton.png", D3DXVECTOR3(150, 320, 0), 1.0f);
	sInstructions = m_pSpriteManager->LoadSprite("MenuSprites\\instructionsButton.png", D3DXVECTOR3(200, 420, 0), 1.0f);
	sViewHighScore = m_pSpriteManager->LoadSprite("MenuSprites\\highScores.png", D3DXVECTOR3(170, 520, 0), 1.0f);
	sQuit = m_pSpriteManager->LoadSprite("MenuSprites\\quitButton.png", D3DXVECTOR3(100, 620, 0), 1.0f);
	sMenuTerminal = m_pSpriteManager->LoadSprite("MenuSprites\\Terminal.png", D3DXVECTOR3(750, 400, 0), 1.2f);
	sPlayPic = m_pSpriteManager->LoadSprite("MenuSprites\\TerminalScreens\\TerminalPlay.png", D3DXVECTOR3(-300, -300, 0), 0.8f);
	sCreditPic = m_pSpriteManager->LoadSprite("MenuSprites\\TerminalScreens\\TerminalCredits.png", D3DXVECTOR3(-300, -300, 0), 0.8f);
	sInstructionPic = m_pSpriteManager->LoadSprite("MenuSprites\\TerminalScreens\\TerminalInstructions.png", D3DXVECTOR3(-300, -300, 0), 1.0f);
	sHighscorePic = m_pSpriteManager->LoadSprite("MenuSprites\\TerminalScreens\\TerminalHighscore.png", D3DXVECTOR3(-300, -300, 0), 0.8f);
	sQuitPic = m_pSpriteManager->LoadSprite("MenuSprites\\TerminalScreens\\TerminalQuit.png", D3DXVECTOR3(-300, -300, 0), 0.8f);
	sLogo = m_pSpriteManager->LoadSprite("MenuSprites\\teamLogo.png", D3DXVECTOR3(650, 700, 0), 0.5f);

	// Instruction screen sprites
	sInstructionScreen = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\instructionsMainScreen.png", D3DXVECTOR3(512, 294, 0), 0.8f);
	sHControl = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightControl.png", D3DXVECTOR3(-50, -50, 0), 0.8f);
	sHObjectives = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightObjectives.png", D3DXVECTOR3(-50, -50, 0), 0.9f);
	sHObstacles = m_pSpriteManager->LoadSprite("MenuSprites\\HighlightSprites\\HighlightObstacles.png", D3DXVECTOR3(-50, -50, 0), 1.0f);
	sControls = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\controls.png", D3DXVECTOR3(480, 220, 0), 0.8f);
	sObjectives = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\objectives.png", D3DXVECTOR3(440, 300, 0), 0.4f);
	sObstacles = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\obstacles.png", D3DXVECTOR3(430, 400, 0), 0.5f);
	sObstacleInstructions = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\obstacleInstructions.png", D3DXVECTOR3(512, 294, 0), 0.8f);
	sControlInstructions = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\controlsInstructions.png", D3DXVECTOR3(512, 284, 0), 0.8f);
	sObjectiveInstructions = m_pSpriteManager->LoadSprite("MenuSprites\\Instructions Sprites\\objectiveInstructions.png", D3DXVECTOR3(512, 294, 0), 0.8f);

	// Credits screen sprite
	sCreditScreen = m_pSpriteManager->LoadSprite("MenuSprites\\creditScreen.png", D3DXVECTOR3(512, 310, 0), 0.7f);

	// Highscore screen sprite
	sHighScoreScreen = m_pSpriteManager->LoadSprite("MenuSprites\\highScore.png", D3DXVECTOR3(512, 294, 0), 0.8f);

	// Back Button Sprite
	sBackButton = m_pSpriteManager->LoadSprite("MenuSprites\\backButton.png", D3DXVECTOR3(500, 700, 0), 0.5f);
}

void MainMenu::Update(HWND hWnd, DirectInput* m_pDirectInput)
{
	//Update audio
	m_pSound->Update();

	// Update the timer
	lCurrentTime = (LONG)timeGetTime();
	lMilliseconds = lCurrentTime - lStartTime;
	if(lMilliseconds > 1000)
	{
		//lTotalTime -= 1000;
		sClickTimer++;
		lStartTime = (LONG)timeGetTime();
	}

	// Menu choices
	m_pDirectInput->Update(hWnd);
	if(m_pDirectInput->CheckMouseButtons(0))
	{
		if(sMenuState == MAINMENU)
		{
			// PLAY
			if(m_pSpriteManager->SpriteCollision(sPlayGame, m_pDirectInput->currentMousePos))
			{
				if(!bSoundLoaded)
				{
					m_pSound->MenuButtonClickSFX();
					bSoundLoaded = true;
				}
				sGameState = 1;
			}
			else
				bSoundLoaded = false;

			// INSTRUCTIONS
			if(m_pSpriteManager->SpriteCollision(sInstructions, m_pDirectInput->currentMousePos))
			{
				if(!bSoundLoaded)
				{
					m_pSound->MenuButtonClickSFX();
					bSoundLoaded = true;
				}
				sMenuState = INSTRUCTIONS;
			}
			else
				bSoundLoaded = false;

			// CREDITS
			if(m_pSpriteManager->SpriteCollision(sCredits, m_pDirectInput->currentMousePos))
			{
				if(!bSoundLoaded)
				{
					m_pSound->MenuButtonClickSFX();
					bSoundLoaded = true;
				}
				sMenuState = CREDITS;
			}
			else
				bSoundLoaded = false;

			// HIGH SCORE
			if(m_pSpriteManager->SpriteCollision(sViewHighScore, m_pDirectInput->currentMousePos))
			{
				if(!bSoundLoaded)
				{
					m_pSound->MenuButtonClickSFX();
					bSoundLoaded = true;
				}		
				sMenuState = HIGHSCORE;
			}
			else
				bSoundLoaded = false;

			// QUIT
			if(m_pSpriteManager->SpriteCollision(sQuit, m_pDirectInput->currentMousePos))
			{
				if(!bSoundLoaded)
				{
					m_pSound->MenuButtonClickSFX();
					bSoundLoaded = true;
				}
				PostQuitMessage(0);
			}
			else
				bSoundLoaded = false;
		}
		else
		{
			// BACK BUTTON
			if(m_pSpriteManager->SpriteCollision(sBackButton, m_pDirectInput->currentMousePos))
			{
				//check the timer, if greater than 1 then you can change the state
				// otherwise nothing happens
				if(sClickTimer > 1)
				{
					if(sMenuState > MAINMENU && sMenuState < CONTROLS)
					{
						if(!bSoundLoaded)
						{
							m_pSound->MenuButtonClickSFX();
							bSoundLoaded = true;
						}
						sMenuState = MAINMENU;
						sClickTimer = 0;
					}
					else
					{
						if(!bSoundLoaded)
						{
							m_pSound->MenuButtonClickSFX();
							bSoundLoaded = true;
						}
						sMenuState = INSTRUCTIONS;
						sClickTimer = 0;
					}
				}
			}
			else
				bSoundLoaded = false;

			if(sMenuState == INSTRUCTIONS)
			{
				///////////Collision Detection for all Instructions Screen sprites!///////////////////
				// CONTROLS
				if(m_pSpriteManager->SpriteCollision(sControls, m_pDirectInput->currentMousePos))
				{
					if(!bSoundLoaded)
					{
						m_pSound->MenuButtonClickSFX();
						bSoundLoaded = true;
					}
					sMenuState = CONTROLS;
				}
				else
					bSoundLoaded = false;
				// OBJECTIVES
				if(m_pSpriteManager->SpriteCollision(sObjectives, m_pDirectInput->currentMousePos))
				{
					if(!bSoundLoaded)
					{
						m_pSound->MenuButtonClickSFX();
						bSoundLoaded = true;
					}
					sMenuState = OBJECTIVES;
				}
				else
					bSoundLoaded = false;
				// OBSTACLES
				if(m_pSpriteManager->SpriteCollision(sObstacles, m_pDirectInput->currentMousePos))
				{
					if(!bSoundLoaded)
					{
						m_pSound->MenuButtonClickSFX();
						bSoundLoaded = true;
					}
					sMenuState = OBSTACLES;
				}
				else
					bSoundLoaded = false;
			///////////Collision Detection for all Instructions Screen sprites!///////////////////
			}
		}
	}

	if(sMenuState == INSTRUCTIONS)
	{
		if( m_pDirectInput->currentMousePos.x >= 391.0 && m_pDirectInput->currentMousePos.x <= 581.0 &&
			m_pDirectInput->currentMousePos.y >= 219.0 && m_pDirectInput->currentMousePos.y <= 267.0 )
		{
			m_pSpriteManager->ModifySpritePos(sHControl, 450, 220);
		}
		else if( m_pDirectInput->currentMousePos.x >= 386.0 && m_pDirectInput->currentMousePos.x <= 581.0 &&
				 m_pDirectInput->currentMousePos.y >= 302.0 && m_pDirectInput->currentMousePos.y <= 355.0 )
		{
			m_pSpriteManager->ModifySpritePos(sHObjectives, 440, 310);	
		}
		else if( m_pDirectInput->currentMousePos.x >= 365.0 && m_pDirectInput->currentMousePos.x <= 612.0 &&
				 m_pDirectInput->currentMousePos.y >= 395.0 && m_pDirectInput->currentMousePos.y <= 427.0 )
		{
			m_pSpriteManager->ModifySpritePos(sHObstacles, 440, 400);	
		}
		else
		{
			m_pSpriteManager->ModifySpritePos(sHControl, -50, -50);
			m_pSpriteManager->ModifySpritePos(sHObjectives, -50, -50);
			m_pSpriteManager->ModifySpritePos(sHObstacles, -50, -50);
		}
	}
}

void MainMenu::Render(IDirect3DDevice9* m_pD3DDevice)
{	
	switch(sMenuState)
	{
	case MAINMENU:
		// Draw menu sprites if in state 0:main menu
		for(short i = 0; i < 18; i++)
			m_pSpriteManager->DrawSprite(i);
		break;
	case INSTRUCTIONS:
		// Render the instruction sprites if in state 1 
		for(short i = sInstructionScreen; i <= sObstacles; i++)
			m_pSpriteManager->DrawSprite(i);
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	case CREDITS:
		// Render the credits sprites if in state 2
		m_pSpriteManager->DrawSprite(sCreditScreen);
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	case HIGHSCORE:
		// Render the high score screen sprites if in state 3
		m_pSpriteManager->DrawSprite(sHighScoreScreen);
		//m_pText->SetHighScore(m_pHighScore->strLName);
		m_pHighScore->ShowBoard();
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	case CONTROLS:
		// Render controls
		m_pSpriteManager->DrawSprite(sControlInstructions);
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	case OBJECTIVES:
		// Render objectives
		m_pSpriteManager->DrawSprite(sObjectiveInstructions);
		m_pSpriteManager->DrawSprite(sBackButton);
		break;
	case OBSTACLES:
		// Render obstacles
		m_pSpriteManager->DrawSprite(sObstacleInstructions);
		m_pSpriteManager->DrawSprite(sBackButton);
	}
}

short MainMenu::GetStateMenu(void)
{
	return sGameState;
}

void MainMenu::GetPlayGameHighlight(void)
{
	m_pSpriteManager->ModifySpritePos(sHPlayGame, 130, 220);
	m_pSpriteManager->ModifySpritePos(sPlayPic, 790, 255);
}

void MainMenu::GetCreditsHighlight(void)
{
	m_pSpriteManager->ModifySpritePos(sHCredits, 125, 320);
	m_pSpriteManager->ModifySpritePos(sCreditPic, 770, 255);
}

void MainMenu::GetInstructionsHighlight(void)
{
	m_pSpriteManager->ModifySpritePos(sHInstructions, 200, 420);
	m_pSpriteManager->ModifySpritePos(sInstructionPic, 780, 295);
}

void MainMenu::GetHighScoreHighlight(void)
{
	m_pSpriteManager->ModifySpritePos(sHHighScore, 175, 520);
	m_pSpriteManager->ModifySpritePos(sHighscorePic, 770, 255);
}

void MainMenu::GetQuitHighlight(void)
{
	m_pSpriteManager->ModifySpritePos(sHQuit, 100, 620);
	m_pSpriteManager->ModifySpritePos(sQuitPic, 770, 255);
}

void MainMenu::RemoveHighlights(void)
{
	m_pSpriteManager->ModifySpritePos(sHPlayGame, -50, -50);
	m_pSpriteManager->ModifySpritePos(sHCredits, -50, -50);
	m_pSpriteManager->ModifySpritePos(sHInstructions, -50, -50);
	m_pSpriteManager->ModifySpritePos(sHHighScore, -50, -50);
	m_pSpriteManager->ModifySpritePos(sHQuit, -50, -50);
	m_pSpriteManager->ModifySpritePos(sPlayPic, -300, -300);
	m_pSpriteManager->ModifySpritePos(sCreditPic, -300, -300);
	m_pSpriteManager->ModifySpritePos(sInstructionPic, -300, -300);
	m_pSpriteManager->ModifySpritePos(sHighscorePic, -300, -300);
	m_pSpriteManager->ModifySpritePos(sQuitPic, -300, -300);
}
void MainMenu::DestroyMenu(void)
{
	for(short i = sBackButton; i >= sTitleBar; i--)
		m_pSpriteManager->DestroySprite(i);
}